import 'package:cat_eye/bean/home_body_bean.dart';
import 'package:cat_eye/bean/sq_lite_bean.dart';
import 'package:cat_eye/config/string_config.dart';
import 'package:cat_eye/root_page/home_page/video_details_widget.dart';
import 'package:cat_eye/sqflite/sqLite_manager.dart';
import 'package:cat_eye/util/date_util.dart';
import 'package:cat_eye/util/expand_more_text_widget.dart';
import 'package:cat_eye/util/util.dart';
import 'package:flutter/material.dart';

/*
* @Package: root_page.find_page
* @ClassName: find_special_details_list_widet
* @Author: szj
* @CreateDate: 8/4/21 3:22 PM
* TODO 发现-专题-详情-列表 
*/
class FindSpecialDetailsListWidget extends StatelessWidget {
  final ItemList item;

  const FindSpecialDetailsListWidget({Key key, @required this.item})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.only(left: 10, right: 10),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          _headDetails(),

          //可折叠文字
          ExpandMoreTextWidget(
            item.data.content.data.description,
            style: TextStyle(fontSize: 14, color: Colors.black38),
          ),

          //标题类型
          _title(),

          //Video视频
          _video(context),

          //底部关注分享按钮
          _bottomLayout(context),

          //分割线
          _divider(),
        ],
      ),
    );
  }

  /// 分割线
  Widget _divider() {
    return Padding(
      padding: EdgeInsets.only(bottom: 5),
      child: Divider(
        color: Colors.black26,
        height: 1.5,
      ),
    );
  }

  Widget _bottomLayout(BuildContext context) {
    return Padding(
      padding: EdgeInsets.only(top: 5, bottom: 5),
      child: Row(
        children: [
          _row(context, Icons.follow_the_signs, StringConfig.FOLLOW, type: 1),
          Padding(
            padding: EdgeInsets.only(left: 30),
            child: _row(context, Icons.comment, StringConfig.COMMENT),
          ),
          Padding(
            padding: EdgeInsets.only(left: 30),
            child: _row(context, Icons.share, StringConfig.COMMENT, type: 3),
          ),
        ],
      ),
    );
  }

  Widget _row(BuildContext context, IconData icon, String text,
      {int type = -1}) {
    return InkWell(
      onTap: () => buildFunction(context, type),
      child: Row(
        children: <Widget>[
          Icon(
            icon,
          ),
          Padding(
            padding: EdgeInsets.only(left: 3),
            child: Text(
              text,
              style: TextStyle(color: Colors.black, fontSize: 13),
            ),
          ),
        ],
      ),
    );
  }

  //暂无数据
  buildFunction(BuildContext context, int type) {
    if (type == 1) {
      //关注
      var bean = SqLiteBean(
          "",
          item.data.content.data.cover.feed,
          item.data.header.icon,
          item.data.content.data.title,
          formatDateMsByYMDHM(item.data.header.time),
          formatDateMsByMS(item.data.content.data.duration * 1000),
          item.data.content.data.playUrl,
          item.data.content.data.cover.blurred);

      SQLiteManager.getInstance().insert(bean, context);
    } else if (type == 3) {
      //分享
      share(
          item.data.content.data.title, item.data.content.data.webUrl.forWeibo);
    } else {
      //评论
      snackBar(context, StringConfig.NO_FUNCTION);
    }
  }

  Widget _video(BuildContext context) {
    return InkWell(
      onTap: () => Navigator.push(context,
          router(widget: VideoDetailsWidget(data: item.data.content.data))),
      child: Padding(
        padding: EdgeInsets.only(top: 4, bottom: 4),
        child: Stack(
          children: [
            cacheImage(
              item.data.content.data.cover.feed,
              borderRadius: BorderRadius.circular(8),
            ),
            Positioned(
                right: 10,
                bottom: 10,
                child: Container(
                  width: 40,
                  height: 25,
                  alignment: Alignment.center,
                  padding: EdgeInsets.all(2),
                  margin: EdgeInsets.all(2),
                  decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(8),
                      color: Colors.black.withAlpha(100)),
                  child: text(
                      formatDateMsByMS(item.data.content.data.duration * 1000),
                      fontColor: Colors.white),
                ))
          ],
        ),
      ),
    );
  }

  Widget _title() {
    return Wrap(
      crossAxisAlignment: WrapCrossAlignment.start,
      children: item.data.content.data.tags
          .map((e) => Container(
                padding: EdgeInsets.all(4),
                margin: EdgeInsets.all(2),
                decoration: BoxDecoration(
                  color: Colors.blue.withAlpha(50),
                  borderRadius: BorderRadius.circular(4),
                ),
                child: text(e.name, fontColor: Colors.blue),
              ))
          .toList(),
    );
  }

  //最上面作者详情
  Widget _headDetails() {
    return Row(
      children: [
        Padding(
          padding: EdgeInsets.only(right: 10, bottom: 3),
          child: cacheImage(item.data.header.icon,
              width: 44, height: 44, borderRadius: BorderRadius.circular(22)),
        ),
        Expanded(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              text(item.data.header.issuerName, fontWeight: FontWeight.bold),
              text(item.data.content.data.title),
              text(formatDateMsByYMDHM(item.data.header.time),
                  fontColor: Colors.black26),
            ],
          ),
        ),
      ],
    );
  }
}
